<?php

namespace AuctionBack\Controller;

use AuctionCore\System\AuctionException;
use AuctionCore\CarDealer\BailList;
use AuctionCore\Common\Page;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\CarDealer\AssetCenter;
use AuctionCore\Account\Back\Employee;

/**
 * 保证金管理
 */
class BailController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    //保证金列表 
    public function index()
    {
        $p = $_REQUEST['p'];
        $p = (intval($p) < 2 ? 1 : $p);
        $limit = 10;
        $dealer_id = I("get.dealer_id", '');
        $contact_person = I("get.contact_person", '');
        $contact_mobile = I("get.contact_mobile", '');
        $params = [
            'dealer_id' => $dealer_id,
            'contact_person' => $contact_person,
            'contact_mobile' => $contact_mobile,
        ];
        $dul = new BailList();
        $data = $dul->get($p, $limit, $params);

        $page = new Page($data['count'], $limit);
        $page_data = $page->page_data();

        $e = new Employee($this->user_info['username']);
        $chakan_auth = $e->check_auth("BaoZhengJinChaKanXiangQing");//查看
        $recharge_auth = $e->check_auth("BaoZhengJinChongZhi");     //充值
        $extract_auth = $e->check_auth("BaoZhengJinTiQu");          //提取
        $this->assign('chakan_auth', $chakan_auth);
        $this->assign('recharge_auth', $recharge_auth);
        $this->assign('extarct_auth', $extract_auth);

        //dump($list);exit;
        $this->assign('params', $params);
        $this->assign('list', $data['list']);
        $this->assign('p', $p);
        $this->assign('page_data', $page_data);
        $this->display();
    }

    //保证金详情
    public function detail()
    {

        $dealer_id = I('request.dealer_id/d', 0);
        if ($dealer_id < 1) {
            $this->helpJsRedirect('车商ID错误');
        }

        $dealer = new Dealer($dealer_id);

        $info = $dealer->baseInfo();
        //检测权限

        $e = new Employee($this->user_info['username']);

        $recharge_auth = $e->check_auth("BaoZhengJinChongZhi");
        $this->assign('info', $info);
        $use_type = C('USE_TYPE');
        $this->assign('use_type', $use_type);
        $this->assign('recharge_auth', $recharge_auth);
        $this->display();
    }

    //保证金使用情况
    public function bailStatus()
    {
        //检测权限
        $e = new Employee($this->user_info['username']);
        $auth = $e->check_auth("BaoZhengJinShiYongQingKuan");
        if ($auth === false) {
            $this->helpJsonResult(500, '没有权限');
        }
        $dealer_id = I('request.dealer_id/d', 0);
        $p = I('request.p/d', 1);
        $limit = 10;

        $asset = new AssetCenter($dealer_id);
        $amount_info = $asset->bailSituation();
        $amount_info['bail_amount'] = intval($amount_info['bail_amount']);
        $amount_info['available_amount'] = intval($amount_info['available_amount']);
        $amount_info['freeze_amount'] = intval($amount_info['freeze_amount']);
        $amount_info['indemnity_amount'] = intval($amount_info['indemnity_amount']);
        $amount_info['minus_amount'] = intval($amount_info['minus_amount']);
        $params = array();
        $params['use_type'] = 2;
        $params['is_free'] = 0;
        $result = $asset->get_bail_logs($p, $limit, $params);
        //页控
        $page = new Page($result['total'], $limit);
        $page_data = $page->page_data();
        $this->assign('p', $p);
        $this->assign('page_data', $page_data);
        $use_type = C('USE_TYPE');
        $this->assign('use_type', $use_type);
        $this->assign('total', $result['total']);
        $this->assign('freeze_log', $result['logs']);
        $this->assign('amount_info', $amount_info);
        $this->helpJsonResult(200, 'success', $this->fetch("Bail/bail_status"));
        //$this->display();
    }

    //基本信息
    public function base_info()
    {
        $dealer_id = $_REQUEST['dealer_id'];
        $d = new Dealer($dealer_id);
        $b_info = $d->baseInfo();

        //dump($b_info);
        $add = new \AuctionCore\Other\Address();
        if (intval($b_info['province']) > 0) {
            $sheng = $add->get_name_by_code($b_info['province']);
            $sheng = $sheng . ($b_info['province'] == $b_info['city'] ? '市' : '省');
        } else {
            $sheng = '';
        }
        if (intval($b_info['city']) > 0) {
            $shi = $add->get_name_by_code($b_info['city']);
            $shi = $shi . ($b_info['province'] == $b_info['city'] ? '' : '市');
        } else {
            $shi = '';
        }

        $b_info['area'] = $add->get_name_by_code($b_info['area']);
        $b_info['show'] = $sheng . $sheng . $b_info['area'];
        //dump($b_info);

        $COMPANY_TYPE = c('COMPANY_TYPE');
        $DEALER_LEVEL = C('DEALER_LEVEL');
        $this->assign('DEALER_LEVEL', $DEALER_LEVEL);
        $this->assign('COMPANY_TYPE', $COMPANY_TYPE);
        $this->assign('b_info', $b_info);
        //json输出
        $this->helpJsonResult(200, 'success', $this->fetch());
    }

    //历史记录
    public function his_log()
    {
        $p = I('request.p', 1);
        $limit = 10;
        $params = array();
        $params['use_type'] = I('post.use_type');
        $params['use_time'] = I('post.use_time', '');
        $dealer_id = I('request.dealer_id/d');
        $asset = new AssetCenter($dealer_id);
        $result = $asset->get_bail_logs($p, $limit, $params);
        $page = new Page($result['total'], $limit);
        $page_data = $page->page_data();
        $this->assign('p', $p);
        $this->assign('page_data', $page_data);
        $use_type = C('USE_TYPE');
        $this->assign('total', $result['total']);
        $this->assign('use_type', $use_type);
        $freeze_log = array();
        foreach ($result['logs'] as $k => $v) {
            $freeze_log[$k]['bl_id'] = $v['bl_id'];
            $freeze_log[$k]['dealer_id'] = $v['dealer_id'];
            $freeze_log[$k]['use_time'] = $v['use_time'];
            $freeze_log[$k]['use_type'] = $v['use_type'];
            $freeze_log[$k]['use_detail'] = $v['use_detail'];
            $freeze_log[$k]['occur_money'] = intval($v['occur_money']);
            $freeze_log[$k]['remain_amount'] = intval($v['remain_amount']);
            $freeze_log[$k]['recharge_method'] = $v['recharge_method'];
            $freeze_log[$k]['createtime'] = $v['createtime'];
            $freeze_log[$k]['is_free'] = $v['is_free'];
        }
        $this->assign('freeze_log', $freeze_log);
        //json输出
        $this->helpJsonResult(200, 'success', $this->fetch());
    }

    //保证金充值提交
    public function add_price()
    {
        try {
            $price = I('post.price');
            $dealer_id = I('request.dealer_id');
            $d = new Dealer($dealer_id);
            $detail = $d->detail();
            if ($detail['level'] == 0) {
                $this->helpJsonResult(400, "车商未认证");
            }
            if ($detail['is_agree_agreement'] == 0) {
                $this->helpJsonResult(400, "车商未签约");
            }
            $asset = new AssetCenter($dealer_id);
            $rechargeData = [
                'occur_money' => $price,
                'recharge_method' => 3,
                'use_detail' => '保证金充值',
                'emp_id' => $this->user_info['emp_id'],
                'emp_name' => $this->user_info['real_name'],
            ];
            $info = $asset->bail_recharge($rechargeData);
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => ''));

            \AuctionCore\CarDealer\Dealer::clear_dealer_bail_cache($dealer_id);

            $logArr = [
                'emp_id' => $this->user_info['emp_id'],
                'action' => '车商' . $dealer_id . '-' . $detail['contact_person'] . '充值保证金 ' . $price . '元',
            ];
            \AuctionCore\System\SystemLog::add($logArr);

            exit;
        } catch (AuctionException $e) {
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }


    /**
     * 提取保证金
     */
    public function get_price()
    {
        try {
            $price = I('post.price');
            $dealer_id = I('request.dealer_id');
            $d = new Dealer($dealer_id);
            $detail = $d->detail();
            if ($detail['level'] == 0) {
                $this->helpJsonResult(400, "车商未认证");
            }
            if ($detail['is_agree_agreement'] == 0) {
                $this->helpJsonResult(400, "车商未签约");
            }
            $asset = new AssetCenter($dealer_id);
            //$extract_money, $extract_method = 3, $msg = '保证金提取'
            $emp_id = $this->user_info['emp_id'];
            $emp_name = $this->user_info['real_name'];
            $asset->bail_extract($price, 3, "保证金提取", $emp_id, $emp_name);
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => ''));

            \AuctionCore\CarDealer\Dealer::clear_dealer_bail_cache($dealer_id);

            $logArr = [
                'emp_id' => $this->user_info['emp_id'],
                'action' => '车商' . $dealer_id . '-' . $detail['contact_person'] . '提取保证金 ' . $price . '元',
            ];
            \AuctionCore\System\SystemLog::add($logArr);

            exit;
        } catch (AuctionException $e) {
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }

    /**
     * 获取车商信息
     */
    public function get_dealer_info()
    {
        $dealer_id = I('request.dealer_id/d', 0);
        if ($dealer_id < 1) {
            $this->helpJsRedirect('车商ID错误');
        }
        $dealer = new Dealer($dealer_id);
        $info = $dealer->baseInfo();
        $info['bail_amount'] = intval($info['bail_amount']);
        $info['minus_amount'] = intval($info['minus_amount']);
        echo json_encode($info);
    }

    //获取线下
    public function offline_logs()
    {
        $p = I('get.p/d', 1);
        $limit = 10;
//        //分公司列表
//        $bc_list = $this->get_branch_company_list();
//        if(count($bc_list)>1){
//            $bc_id = I('get.bc_id',-1);
//        }else{
//            $key = array_keys($bc_list);
//            $bc_id =$key['0'];
//        }
        $params = array(
            'dealer_id' => I('get.dealer_id', ''),
            'real_name' => I('get.real_name', ''),
            //'bc_id' => $bc_id,
            'mobile' => I('get.mobile', ''),
            'start_time' => I('get.start_time', ''),
            'end_time' => I('get.end_time', ''),
        );

        $Bail = new BailList();
        $result = $Bail->get_offline_logs($p, $limit, $params);
        $Page = new Page($result['total'], $limit);
        $show = $Page->platform_show();

        //$this->assign('bc_list', $bc_list);
        $this->assign('list', $result['list']);
        $this->assign('params', $params);
        $this->assign('page', $show);
        $this->display();
    }

    //获取拉卡拉充值记录
    public function third_party()
    {
        $p = I('get.p/d', 1);
        $t = I("get.t/d");
        $limit = 10;
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        if (count($bc_list) > 1) {
            $bc_id = I('get.bc_id', -1);
        } else {
            $key = array_keys($bc_list);
            $bc_id = $key['0'];
        }
        $params = array(
            'bc_id' => $bc_id,
            'dealer_id' => I('get.dealer_id', ''),
            'real_name' => I('get.real_name', ''),
            'recharge_method' => $t,
            'mobile' => I('get.mobile', ''),
            'order_no' => I('get.order_no', ''),
            'start_time' => I('get.start_time', ''),
            'end_time' => I('get.end_time', ''),
            'transaction_id' => I('get.transaction_id', ''),
        );

        $Bail = new BailList();
        $result = $Bail->get_lakala_logs($p, $limit, $params);
        $Page = new Page($result['total'], $limit);
        $show = $Page->platform_show();
        $this->assign('bc_list', $bc_list);
        $branch_id = $this->user_info['branch_id'];
        $this->assign('branch_id', $branch_id);
        $this->assign('list', $result['list']);
        $this->assign('params', $params);
        $this->assign('page', $show);
        if ($t == 1)
            $this->display("Bail/alipay_log");
        else
            $this->display("Bail/lakala_logs");
    }

}
